package com.zsrt.java.day20161229.wrl.homework;

import java.util.Scanner;

import com.zsrt.java.day20161229.wrl.demo04.UserEntity;

public class Game {
	
	/**
	 * 1：Scanner：3位的整数（455）
	 * 2：根据最后一位数（5）跟 4 求模
	 * 求模的结果：
	 * 	1	算+	第一个数+第二个数+第三个数 （4+5+5=14）
	 * 	2	算-
	 * 	3	算*
	 * 	0	算/
	 * 3：加减乘除都要封装到方法里，如  add(int a,int b,int c)
	 * 
	 * 4:多次执行，可以手动退出
	 * 	game over，are you sure paly again?（y/n）
	 * 
	 * 5：用junit4测试
	 * 
	 * 6：玩之前：输入用户名和密码，验证登录	 
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		UserEntity user = new UserEntity();
		
		System.out.println("please input username：");
		String username = input.next();
		System.out.println("please input password：");
		String password = input.next();
		
		if(user.login(username, password)){
			new Game().xx(input);
		}
		
		
	}
	
	public void xx(Scanner input){
		boolean judge = true;
		while(judge){
			System.out.print("please input a threeDigits(100<=number<=999)：");
//			System.out.println("请输入一个3位数( 100 <= number <= 999 )：");
			int num  = input.nextInt();
			
			//判断是否为3位的整数
			if(num<100||num>999){
				System.out.println("input error,please input again later!");
//				System.out.println("你输入的不是3位的整数！");
				continue;
			}
			int n1 = num/100;
			int n2 = num%100/10;
			int n3 = num%10;
			int index = num%10%4;
			String flag = "";
			
			int result = 0;
			switch (index) {
			case 1:	
				result = add(n1,n2,n3);	
				flag = "+";
				break;
			case 2:	
				result = sub(n1,n2,n3);	
				flag = "-";
				break;
			case 3:	
				result = mul(n1,n2,n3);	
				flag = "x";
				break;
			case 0:	
				result = div(n1,n2,n3);	
				flag = "÷";
				break;
			}
			System.out.println(n1+flag+n2+flag+n3+"= "+result);
			System.out.println("game over，are you sure paly again?（y/n）");
			judge = yesOrNo(input, judge);
		}
		input.close();
		System.out.println("Byebye...");
	}

	public static boolean yesOrNo(Scanner input, boolean judge) {
		String end = input.next();
		if(end.equalsIgnoreCase("n")){
			judge = false;
		}else if(end.equalsIgnoreCase("y")){
			judge = true;
		}else{
//			System.out.println("输入有误，请重新确认！");
			System.out.println("input error,please input again later!");
			System.out.println("game over，are you sure paly again?（y/n）");
			
			//递归调用
			yesOrNo(input, judge);		
		}
		return judge;
	}

	private static int add(int n1, int n2, int n3) {
		int result = n1+n2+n3;
		return result;
	}

	private static int sub(int n1, int n2, int n3) {
		int result = n1-n2-n3;
		return result;
	}

	private static int mul(int n1, int n2, int n3) {
		int result = n1*n2*n3;
		return result;
	}

	private static int div(int n1, int n2, int n3) {
		int result = n1/n2/n3;
		return result;
	}

}
